package edu.cuit.avatar.exception;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.Reader;
import java.util.Scanner;

/**
 * Object-----
 *      Throwable-----
 *          Exception
 *              IOException
 *                  FileNotFoundException
 *              ClassNotFoundException
 *              RuntimeException
 *                  ArithmeticException
 *                  StringIndexOutOfBoundsException
 *                  IndexOutOfBoundsException
 *                  NullPointerException
 *                  IllegalArgumentsException
 *                  .....
 *         Error: JVM虚拟机抛出的系统内部错误
 *              VirtualMachineError
 *              StackOverflowError
 * @author <a href="mailto:1020zhaodan@163.com">Adan</a>
 * @version 1.0
 * @date 2025/7/11 10:54
 */
public class QuotientWithException2 {

    public static void main(String[] args) {
        //test();
        System.out.println("请输入两个整数:");
        try(Scanner input = new Scanner(System.in)){
            int number1 = input.nextInt();
            int number2 = input.nextInt();
//            if(number2 != 0) {
//                //如果number2 = 0,会抛出 ArithmeticException
//                System.out.printf("%d / %d = %d", number1, number2, number1 / number2);
//            }
            //方法调用者可以捕获相关异常,对异常情况进行处理
            int result = quotient(number1, number2);
            System.out.printf("%d / %d = %d", number1, number2, result);
        } catch (Exception e) {  //异常对象包含关于异常有价值的信息
            //打印堆栈跟踪异常信息
            e.printStackTrace();
            //System.out.println(e.getMessage()); //描述该异常对象的信息
            //System.out.println(e); //异常全限定名: getMessage()
            e.getStackTrace();
        }
        System.out.println("程序运行结束");
    }


    public static int quotient(int number1, int number2) throws Exception{
        if(number2 == 0){
            /*
             * 当除数为0:方法抛出异常,方法的调用者可以捕获及处理该异常
             * 当方法抛出异常后,后续逻辑终止
             *
             * 异常是一种对象：表示阻止程序正常进行的错误或情况
             */
            throw new Exception("除数为0");
        }
        return number1 / number2;
    }

    public static void test(){
        try {
            Reader reader = new BufferedReader(new FileReader("D:/peppa.txt"));
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
}
